package com.dss.dsc.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;

import com.dss.dsc.serlvet.ExportSlideServlet;
import com.dss.dsc.serlvet.GetFileListServlet;
import com.dss.dsc.serlvet.SysForViewSaveServlet;
import com.dss.dsc.serlvet.SystemForViewServlet;

public class WebAppServer {
	public static void main(String[] args) throws Exception {
		Server server = new Server(8080);

		WebAppContext context = new WebAppContext();
		context.setContextPath("/");

		context.addServlet(new ServletHolder(new SystemForViewServlet()), "/open");
		context.addServlet(new ServletHolder(new SysForViewSaveServlet()), "/save");
		context.addServlet(new ServletHolder(new GetFileListServlet()), "/files");
		context.addServlet(new ServletHolder(new ExportSlideServlet()), "/slide");
		context.addServlet(new ServletHolder(new ExportSlideServlet()), "/ss");
		String url = readIni("rsc.ini") + "WebRoot";//"F:/DSC/WEBeditor/worskpace/webEditor/WebRoot";//readIni("rsc.ini");
		context.setResourceBase(url);
		context.setParentLoaderPriority(true);
		server.setHandler(context);

		server.start();
		server.join();
	}

	public static String readIni(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			String url = "";
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				System.out.println("line " + line + ": " + tempString);
				url += tempString;
				line++;
			}
			reader.close();
			return url;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return null;
	}
}
